public class Test {

    //插入排序
    public static void insertSort(int [] arr){
        for(int i = 1; i < arr.length ; i++){
            int tmp = arr[i];
            int j = i - 1;
            for(; j >= 0; j--){
                if(tmp < arr[j]){
                    arr[j+1] = arr[j];
                } else {
                    break;
                }
            }
            arr[j+1] = tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] arr){
        int gap = arr.length;
        while(gap > 1){
            gap /= 2;
            for(int i = gap; i < arr.length; i++){
                int tmp = arr[i];
                int j = i - gap;
                for(; j > 0 ; j -= gap){
                    if(tmp < arr[j]){
                        arr[j+1] = arr[j];
                    } else {
                        break;
                    }
                }
                arr[j+gap] = tmp;
            }
        }
    }

    //选择排序
    public static void selectSort(int[] arr){
        for(int i = 0; i < arr.length-1; i++){
            int minIndex = i;
            for(int j = i+1; j < arr.length; j++){
                if(arr[j] < arr[minIndex]){
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }

    //堆排序
    public static void heapSort(int[] arr){
        createBigHeap(arr);
        int end = arr.length-1;
        for(int i = end; i > 0; i--){
            swap(arr,i,0);
            siftDown(arr,0,i);
        }
    }
    public static void createBigHeap(int[] arr){
        for(int parent = (arr.length-1-1)/2; parent >= 0; parent--){
            siftDown(arr,parent,arr.length);
        }
    }
    public static void siftDown(int[] arr, int parent, int end){
        int child = parent * 2 + 1;
        while(child < end){
            //令child指向左右孩子中最大的
            if(child+1 < end && arr[child] < arr[child+1]){
                child++;
            }
            if(arr[parent] < arr[child]){
                swap(arr,parent,child);
                parent = child;
                child = parent*2 + 1;
            } else {
                break;
            }
        }
    }

    //冒泡排序
    public static void bubbleSort(int[] arr){
        for(int i = 0; i < arr.length - 1; i++){
            for(int j = 0; j < arr.length - 1 - i; j++){
                if(arr[j] > arr[j+1]){
                    swap(arr, j, j+1);
                }
            }
        }
    }

    //快速排序
    public static void quickSort(int[] arr){
        quick(arr, 0, arr.length-1);
    }
    public static void quick(int[] arr, int left, int right){
        if(left >= right) return;
        int provit = partition(arr, left, right);
        quick(arr, 0, provit-1);
        quick(arr, provit+1, right);
    }

    public static int partition(int[] arr, int left, int right){
        int tmp = arr[left];
        while(left < right){
            while(left < right && arr[right] > tmp){
                right--;
            }
            arr[left] = arr[right];
            while(left < right && arr[left] < tmp){
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = tmp;
        return left;
    }

    public static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {2,6,5,7,9,12,3};
        //insertSort(arr);
        //shellSort(arr);
        //heapSort(arr);
        //bubbleSort(arr);
        quickSort(arr);
        System.out.println("===");
    }
}
